<?php
/**
 * 扫码溯源
 *
 *
 *
 * @copyright  Copyright (c) 中国追溯
 * @since      File available since Release v1.1
 */

use Shopnc\Tpl;

defined('InShopNC') or exit('Access Invalid!');
class scan_codeControl extends mobileControl{

    const SPECIAL = 'jymf';

    public function indexOp(){
        sleep(3);
        $result = urldecode($_POST['result']);
        $info = parse_url($result);
        $host = $info['host'];
        $access_host = array('wx.zhuisusys.com','d.cpzs.org');

        if(!in_array($host,$access_host)){
            output_error('找不到追溯码');
        }

        switch ($info['host']){
            case $access_host[0]:
                $this->scan($info);
                break;
            case $access_host[1]:
                $this->scan1($info);
                break;
        }

        output_error('找不到追溯码');
    }

    /**
     * 扫广州的码
     * @param $info
     */
    private function scan($info){
        $id = trim($info['path'],'/');
        $url = 'http://wx.zhuisusys.com/index/goods_info_ajax/'.$id;
        $res = $this->_get_url_contents($url);
        $res = json_decode($res,true);
        if(empty($res['code']) || ('200'!=$res['code'])){
            output_error('找不到追溯码');
        }
        $new_info = array();
        foreach($res['datas']['info'] as $info){
            if(!empty($info['data'])){
                $new_in = array();
                foreach($info['data']['in'] as $in){
                    $new_in[] = array(
                        'operator_name' => $in['OPERATOR_NAME'],
                        'date' => $in['DATE'],
                    );
                }
                $new_info[] = array(
                    'name' => $info['name'],
                    'in' => $new_in,
                );
            }
        }
        $data = array(
            'trace_code' => $res['datas']['traceCode'],
            'goods_name' => $res['datas']['goodsName'],
            'type' => 1,
            'info' => $new_info,
        );
        output_data($data);
    }

    /**
     * 扫北京的码
     * @param $info
     */
    public function scan1($info=''){

//        $info = array('query'=>'id=1004259238418941');
        $id = explode('=',$info['query'])[1];
        $uuid = md5(uniqid());
        $token = md5($uuid.self::SPECIAL);
        $url = 'https://minit.cpzs.org/api/getInformation';
        $data = array(
            'id' => $id,
//            'jwd' => '113.328031,23.136988',
            'uuid' => $uuid,
            'token' => $token,
        );
        $data = json_encode($data);
//        $res = rkcache($id);
//        if(empty($res)){
            $res = $this->_get_url_contents($url,$data,array('Content-Type:application/json;charset=utf-8'));
//            wkcache($id,$res);
//        }


        $res = json_decode($res,true);
        if(empty($res['Flag']) || (true!=$res['Flag'])){
            output_error('找不到追溯码');
        }

        $resdata = $res['Data'];

        $goods_info = array(
            'name' => '公司名称',
            'in' => array(
                array(
                    'operator_name' => $resdata['Company']['company_name'],
                ),
            ),
        );

        //未验证，返回公司名称和商品名称
        //验证次数，等于0就是未验证
        $cons_count = $resdata['Info']['cons_count'];
        if(0==$cons_count){
            $verifyinfo = array(
                'name' => '验证状态',
                'in' => array(
                    array(
                        'operator_name' => '该商品未验证，请输入验证码进行查询',
                    ),
                ),
            );
            $info = array(
                $goods_info,
                $verifyinfo,
            );
        }else{

            $verifyinfo = array(
                'name' => '验证状态',
                'in' => array(
                    array(
                        'operator_name' => '已验证，该商品已被消费',
                    ),
                ),
            );
            $expense = array(
                'name' => '首次消费信息',
                'in' => array(
                    array(
                        'operator_name' => $resdata['Info']['cons_addr'],
                        'date' => $resdata['Info']['cons_time'],
                    ),
                ),
            );
            $info = array(
                $goods_info,
                $verifyinfo,
                $expense
            );
        }

        $list = array(
            'trace_code' => $resdata['Info']['label_id'],
            'goods_name' => $resdata['Product']['product_name'],
            'type' => 2,
            'cons_count' => $cons_count,
            'cons_code' => $resdata['Info']['cons_code'],   //验证码
            'info' => $info,
        );
//        pr($list);
        output_data($list);
        //pr($list);
    }

    /**
     * 扫北京的码，输入验证码进行验证
     */
    public function scen1verifyOp(){
        $id = $_POST['trace_code'];
        $jwd = $_POST['jwd'];
        $uuid = md5(uniqid());
        $token = md5($uuid.self::SPECIAL);
        $url = 'https://minit.cpzs.org/api/verification';

        $data = array(
            'id' => $id,
            'jwd' => $jwd,
            'uuid' => $uuid,
            'token' => $token,
        );
        $data = json_encode($data);
//        $res = rkcache($id);
//        if(empty($res)){
            $res = $this->_get_url_contents($url,$data,array('Content-Type:application/json;charset=utf-8'));
//            wkcache($id,$res);
//        }
        $res = json_decode($res,true);
        if(empty($res['Flag']) || (true!=$res['Flag'])){
            output_error('验证失败');
        }

        output_data('验证成功');
    }

    /**
     * 接口Url
     * @param string $url
     * @return string
     */
    private function _get_url_contents($url,$post_data = array(),$header = array()){
        $ch = curl_init();
        //curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_URL, $url);

        if(!empty($post_data)) {
            curl_setopt($ch, CURLOPT_POST, TRUE);
            if(is_array($post_data)) $post_data = json_encode($post_data);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        }
        if(!empty($header)){
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }

        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }
}